package com.xb.cloud.product.merchant.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.xb.cloud.product.merchant.service.client.MerchantUserAutoLoginService;
import com.xb.cloud.product.merchant.service.client.bo.MerchantUserAutoLoginBo;
import com.xb.cloud.product.service.business.model.AdminUserAutoLogin;
import com.xb.cloud.product.service.business.model.AdminUserAutoLoginExample;
import com.xb.cloud.product.service.business.model.AdminUserAutoLoginExample.Criteria;
import com.xb.cloud.product.service.business.model.mapper.AdminUserAutoLoginMapper;
import com.xb.cloud.service.client.ServiceReturnCodes;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;
@Service
public class MerchantUserAutoLoginServiceImpl extends AbstractService implements MerchantUserAutoLoginService{

	@Resource 
	AdminUserAutoLoginMapper adminUserAutoLoginMapper;
	
	
	@Override
	public ServiceReturnInfo getByAut(String autoUserToken) {
	
		MerchantUserAutoLoginBo endMerchantUserAutoLoginBo = new MerchantUserAutoLoginBo();
		
		AdminUserAutoLoginExample adminUserAutoLoginExample = new AdminUserAutoLoginExample();
		Criteria createCriteria = adminUserAutoLoginExample.createCriteria();
		createCriteria.andAutEqualTo(autoUserToken);
		List<AdminUserAutoLogin> adminUserAutoLogins = adminUserAutoLoginMapper.selectByExample(adminUserAutoLoginExample);
		if(CollectionUtils.isEmpty(adminUserAutoLogins)) {
			return ServiceReturnUtils.success(null);
		}
		BeanUtils.copyProperties(adminUserAutoLogins.get(0), endMerchantUserAutoLoginBo);
		
		return ServiceReturnUtils.success(endMerchantUserAutoLoginBo);
		
	}

	@Override
	public ServiceReturnInfo deleteAutoLogin(MerchantUserAutoLoginBo endMerthantUserAutoLoginBo) {
		AdminUserAutoLoginExample adminUserAutoLoginExample = new AdminUserAutoLoginExample();
        Criteria createCriteria = adminUserAutoLoginExample.createCriteria();
		createCriteria.andAutEqualTo(endMerthantUserAutoLoginBo.getAut()).andAdminUserIdEqualTo(endMerthantUserAutoLoginBo.getAdminUserId())
		.andCompanyIdEqualTo(endMerthantUserAutoLoginBo.getCompanyId());
		adminUserAutoLoginMapper.deleteByExample(adminUserAutoLoginExample);
		return ServiceReturnUtils.success();
	}

	
	
	/**
	 * 
	 *   
	 * @param id
	 * @return   
	 * @see com.xb.cloud.product.merchant.service.client.MerchantUserAutoLoginService#getMerchantUserAutoLoginByEndUserId(java.lang.Long)
	 */
	
	@Override
	public ServiceReturnInfo getMerchantUserAutoLoginByEndUserId(Long id) {
		
		AdminUserAutoLogin adminUserAutoLogin = adminUserAutoLoginMapper.selectByPrimaryKey(id);
		if(adminUserAutoLogin==null) {
			return ServiceReturnUtils.success(null);
		}
		MerchantUserAutoLoginBo endMerchantUserAutoLoginBo = new MerchantUserAutoLoginBo();
		BeanUtils.copyProperties(adminUserAutoLogin, endMerchantUserAutoLoginBo);
		return ServiceReturnUtils.success(endMerchantUserAutoLoginBo);
		
		
	}

	
	
	
	@Override
	public ServiceReturnInfo createMerchantUserAutoLogin(MerchantUserAutoLoginBo merchantUserAutoLoginBo) {
		if(merchantUserAutoLoginBo==null) {
			return ServiceReturnUtils.error(ServiceReturnCodes.PARAM_IS_NULL);
		}
		AdminUserAutoLogin adminUserAutoLogin = new AdminUserAutoLogin();
		BeanUtils.copyProperties(merchantUserAutoLoginBo, adminUserAutoLogin);
		adminUserAutoLoginMapper.insertSelective(adminUserAutoLogin);
		return ServiceReturnUtils.success();
		
	}

	@Override
	public ServiceReturnInfo deleteByAut(String autoUserToken) {
		AdminUserAutoLoginExample adminUserAutoLoginExample = new AdminUserAutoLoginExample();
		Criteria createCriteria = adminUserAutoLoginExample.createCriteria();
		createCriteria.andAutEqualTo(autoUserToken);
		adminUserAutoLoginMapper.deleteByExample(adminUserAutoLoginExample);
		return ServiceReturnUtils.success();
	}

}
